package gov.nhso.prov.acc.utils;

import gov.nhso.prov.acc.core.ConfigBundle;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
	private static final Locale LOCALE_TH = new Locale("TH","th");
	private static final Locale LOCALE_EN = new Locale("EN","en");
	/**
	 * method convert date format
	 * @param type
	 * DB = convert to database
	 * DP = convert to display
	 * PN = convert to period number
	 * @param d : string date
	 * @return string date by type format
	 */
	@SuppressWarnings("deprecation")
	public String convertDate(String type, String d) {
		String result = "";
        try {
        	if (d != null && !d.equals("")) {
				DateFormat f1 = null;
				DateFormat f2 = null;
				Date date = null;
				if (type.equals("DB")) {
			        f1 = new SimpleDateFormat("dd/MM/yyyy");
			        f2 = new SimpleDateFormat("yyyy-MM-dd");
					date = f1.parse(d);
				} else if (type.equals("DP")) {
			        f1 = new SimpleDateFormat("yyyy-MM-dd");
			        f2 = new SimpleDateFormat("dd/MM/yyyy", LOCALE_TH);
					date = f1.parse(d);
					if (!ConfigBundle.getValue("DEPLOY_TYPE").equals("T")){
						date.setYear(date.getYear() + 543);
					}
				} else if (type.equals("DP2")) {
			        f1 = new SimpleDateFormat("yyyy-MM-dd");
			        f2 = new SimpleDateFormat("dd/MM/yyyy", LOCALE_TH);
					date = f1.parse(d);
					date.setYear(date.getYear() + 543);
				} else if (type.equals("PN")) {
					f1 = new SimpleDateFormat("dd/MM/yyyy");
			        f2 = new SimpleDateFormat("yyMM");
					date = f1.parse(d);
				} else if (type.equals("FTH")) {
					f1 = new SimpleDateFormat("yyyy-MM-dd");
			        f2 = new SimpleDateFormat("dd MMMM yyyy", LOCALE_TH);
					date = f1.parse(d);
					date.setYear(date.getYear() + 543);
				} else if (type.equals("TS2D")) {
					f1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    f2 = new SimpleDateFormat("yyyy-MM-dd");
				    date = f1.parse(d);
				} else if (type.equals("D2TS")) {
				    f1 = new SimpleDateFormat("yyyy-MM-dd");
					f2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    date = f1.parse(d);
				}
		        result = f2.format(date);
        	}
        } catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public String convertToDBDate(String date) {
		String result = "";
		Date date1 = null;
		SimpleDateFormat ssdd1 = null;
		SimpleDateFormat ssdd2 = null;
		try {
			if (date != null && !date.equals("")) {
				ssdd1 = new SimpleDateFormat("dd/MM/yyyy", LOCALE_TH);
				date1 = ssdd1.parse(date);
				ssdd2 = new SimpleDateFormat("yyyy-MM-dd", LOCALE_EN);
				result = ssdd2.format(date1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public String getCurrentDate(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyy-MM-dd", LOCALE_EN);
        return ssdd.format(new Date());
    }
	public String getThaiCurrentDate(){
        SimpleDateFormat ssdd = new SimpleDateFormat("dd/MM/yyyy", LOCALE_TH);
        return ssdd.format(new Date());
    }
	public String getCurrentTimeStamp(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LOCALE_EN);
        return ssdd.format(new Date());
    }
	
	public String getCurrentTimeStamp2DB(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyyMMddHHmmssSSS", LOCALE_EN);
        return ssdd.format(new Date());
    }
	
    public String getYearThai2Digits(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yy", LOCALE_TH);
        return ssdd.format(new Date());
    }

    public String getYearThai4Digits(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyy", LOCALE_TH);
        return ssdd.format(new Date());
    }
    
    public String getBudgetYear(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyy", LOCALE_TH);
        SimpleDateFormat ssmm = new SimpleDateFormat("MM", LOCALE_TH);
        int yyyy = Integer.parseInt(ssdd.format(new Date()));
        int mm = Integer.parseInt(ssmm.format(new Date()));
        if (mm > 9) {
        	yyyy = yyyy + 1;
        }
        return String.valueOf(yyyy);
    }
    
    public String changeFormatDate2DDMMYYYY(String date){
    	String result = "";
    	SimpleDateFormat sdf = null;
    	if (!ConfigBundle.getValue("DEPLOY_TYPE").equals("P")) {
        	sdf = new SimpleDateFormat("yyyymmdd",new Locale("TH","th"));
    	} else {
        	sdf = new SimpleDateFormat("yyyymmdd");
    	}
    	if ((date != null && !date.equals("")) && sdf != null) {
    		try {
				Date d = sdf.parse(date);
				sdf.applyPattern("dd/mm/yyyy");
				result = sdf.format(d);
				String[] arrDate = result.split("\\/");
				result = arrDate[0] + "/"+arrDate[1]+"/"+(Integer.parseInt(arrDate[2]) + 543);
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
    
    public String getCurrentFullDate() {
    	SimpleDateFormat ssdd = new SimpleDateFormat("dd MMMM yyyy", LOCALE_TH);
        return ssdd.format(new Date());
    }
    
    public String getMonthThai2Digits(){
        SimpleDateFormat ssdd = new SimpleDateFormat("MM", LOCALE_TH);
        return ssdd.format(new Date());
    }
    
    public String changeFormatDate(String date,String splitStr,String separate){
    	if(!date.equals("")){
        	String spiltDate[] = date.split("\\"+splitStr+"");
        	return spiltDate[0]+separate+spiltDate[1]+separate+""+(Integer.parseInt(spiltDate[2])-543);
    	}else {
    		return "";
    	}
    }

    public String changeFormatDate2YYYYMMDD(String date,String splitStr,String separate){
    	if(!date.equals("")){
        	String spiltDate[] = date.split("\\"+splitStr+"");
        	return (Integer.parseInt(spiltDate[2])-543)+separate+spiltDate[1]+separate+spiltDate[0];
    	}else {
    		return "";
    	}
    }
    
    public String changeFormatFullDate(String date,String splitStr,String separate){
    	String arrMonth[] = {"","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม"};
    	String spiltDate[] = date.split("\\"+splitStr+"");
    	String day = spiltDate[0];
    	if (spiltDate[0].substring(0, 1).equals("0")){
    		day = spiltDate[0].substring(1, 2);
    	}
    	String buffer = arrMonth[Integer.parseInt(spiltDate[1])];
    	return day+separate+buffer+separate+""+spiltDate[2];
    }
    public String changeFormatDigestDate(String date,String splitStr,String separate){
    	String arrMonth[] = {"","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค."};
    	String spiltDate[] = date.split("\\"+splitStr+"");
    	String day = spiltDate[0];
    	if (spiltDate[0].substring(0, 1).equals("0")){
    		day = spiltDate[0].substring(1, 2);
    	}
    	String buffer = arrMonth[Integer.parseInt(spiltDate[1])];
    	return day+separate+buffer+separate+""+(Integer.parseInt(spiltDate[2])+543);
    }
    
    public String changeDateToDB(String date){
    	return "TO_DATE('"+changeFormatDate(date,"/","/")+"','DD.MM.YYYY')";
    }
    public String changeDateToChar(String date){
    	return "TO_CHAR('"+changeFormatDate(date,"/","/")+"','DD.MM.YYYY')";
    }
    public String changeDateFromDB(String date,String separate){
    	String result = "";
    	String[] strBuffer = date.split("\\ ");
    	strBuffer = strBuffer[0].split("-");
    	result = strBuffer[2]+separate+strBuffer[1]+separate+strBuffer[0];
    	return result;
    }
    public String changeFormatDateFromDB(String date,String separate){
    	String result = "";
    	String[] strBuffer = date.split("\\ ");
    	strBuffer = strBuffer[0].split("-");
    	result = strBuffer[0]+separate+strBuffer[1]+separate+strBuffer[2];
    	return result;
    }
    public String changeDateSapToDB(String date){
    	return "TO_DATE('"+date+"','YYYY-MM-DD')";
    }
    public String setTodateToDB(String date){
    	return "TO_DATE('"+date+"','YYYY-MM-DD')";
    }
    
    public String setTimeStamp2DB(String ts){
    	return "TO_TIMESTAMP('" + ts.replaceAll("[^0-9]+", "") + "', 'YYYYMMDDHH24MISSFF3')";
    }
    
    public String setDate2TimeStamp(String d){
    	return "TO_TIMESTAMP('" + changeFormatDate(d,"/","/") + "', 'DD.MM.YYYY')";
    }
    public String setDate2TimeStamp(String d,String time){
    	return "TO_TIMESTAMP('" + changeFormatDate(d,"/","/") +" "+ time +"', 'DD/MM/YYYY HH24MISSFF3')";
    }
    public String changeBcBakToAd(String date){
    	String result = "";
    	String[] sa = null;
    	sa = date.split("\\/");
    	result = sa[0]+"/"+sa[1]+"/"+(Integer.parseInt(sa[2])-543);
    	return result;
    }
    
}
